package com.cloudm.technician.ui.modules.cloudm_box.plate_number

import android.os.Bundle
import android.view.View
import com.cloudm.technician.R
import com.cloudm.technician.data.model.DeviceDTOInfo
import com.cloudm.technician.data.model.ScanResultInfo
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_plate_number.*

/**
 * 作者: wesley
 * 时间: 2017/12/27
 * 描述: 业务逻辑
 */
class PlateNumberFragment : BaseFragment<PlateNumberPresenter>(), PlateNumberContract.View {

    private var license: String? = null

    private val singleAdapter: PlateNumberSingleAdapter by lazy {
        PlateNumberSingleAdapter()
    }

    private val oilAdapter: PlateNumberOilAdapter by lazy {
        PlateNumberOilAdapter()
    }

    override val resource: Int
        get() = R.layout.fragment_plate_number

    companion object {
        fun newInstance(license: String?) = PlateNumberFragment().apply {
            arguments = Bundle().apply {
                putString("info", license)
            }
        }
    }

    override fun initInject() {
        DaggerPlateNumberComponent.builder().appComponent(appComponent).plateNumberPresenterModule(
                PlateNumberPresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        license = bundle.getString("info")
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {
        loadData()
    }

    override fun initListener() {
        radioGroup.setOnCheckedChangeListener { _, checkedId ->
            when (checkedId) {
                R.id.single_data -> {
                    tvPlateNumberDesc.text = mContext.resources.getString(R.string.single_data_desc)
                    showSingleData()
                }

                R.id.oil_data -> {
                    tvPlateNumberDesc.text = mContext.resources.getString(R.string.oil_data_desc)
                    showOilData()
                }
            }
        }
    }

    override fun loadFailure(errorMsg: String) {
        mContext.toast(errorMsg)
    }

    override fun finishSelf() {
        val activity = mContext as? PlateNumberActivity ?: return
        activity.finish()
    }

    override fun showLoadInfoEmpMsg() {
        mContext.toast(mContext.resources.getString(R.string.no_data_for_vehicleInfo))
    }

    override fun buildSingRowAndContentSuccess(listRow: ArrayList<ScanResultInfo>,
                                               listContent: ArrayList<ArrayList<String?>>) {
        singleAdapter.updateRowAndContent(listRow, listContent)
        scrollPanelSingle.setPanelAdapter(singleAdapter)
    }

    override fun buildOilRowAndContentSuccess(listRow: ArrayList<ScanResultInfo>,
                                              listContent: ArrayList<ArrayList<String?>>) {
        oilAdapter.updateRowAndContent(listRow, listContent)
        scrollPanelOil.setPanelAdapter(oilAdapter)
    }


    override fun initHeader(info: DeviceDTOInfo?) {
        info?.let {
            tvPlateNumberInfo.text = mContext.resources.getString(R.string.plate_number_info,
                    it.deviceName ?: "", it.nickname ?: "", it.snId ?: "",
                    it.address ?: mContext.resources.getString(R.string.cannot_load_locaiton_msg))
        }

        when (single_data.isChecked) {
            true -> showSingleData()
            else -> showOilData()
        }
    }

    override fun singleTitleBuildSuccess(list: ArrayList<ScanResultInfo>) {
        singleAdapter.updateColumdata(list)
        scrollPanelSingle.setPanelAdapter(singleAdapter)
    }

    override fun OilTitleBuildSuccess(list: ArrayList<ScanResultInfo>) {
        oilAdapter.updateColumdata(list)
        scrollPanelOil.setPanelAdapter(oilAdapter)
    }

    private fun showSingleData() {
        scrollPanelSingle.visibility = View.VISIBLE
        scrollPanelOil.visibility = View.GONE
    }

    private fun showOilData() {
        scrollPanelSingle.visibility = View.GONE
        scrollPanelOil.visibility = View.VISIBLE
    }

    private fun loadData() {
        presenter?.getDataString(license)
    }

    fun refresh() {
        loadData()
    }
}